import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../../services/ScreenAdapter.dart';

import 'Home.dart';
import 'Category.dart';
import 'Cart.dart';
import 'User.dart';

// 首页底部导航栏
class BottomTabs extends StatefulWidget {
  final index;
  BottomTabs({this.index = 2});

  @override
  State<StatefulWidget> createState() => BottomTabsState(this.index);
}

class BottomTabsState extends State {
  int _currentIndex;
  var _pageController;
  List<Widget> _pageList = [HomePage(), CategoryPage(), CartPage(), UserPage()];

  @override
  void initState(){
    super.initState();
    _pageController = new PageController(initialPage: this._currentIndex);
  }

  BottomTabsState(index) {
    this._currentIndex = index;
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: _currentIndex == 2 || _currentIndex == 3 ? null : AppBar(
          leading: IconButton(
            icon: Icon(Icons.center_focus_weak,size:28,color:Colors.black87),
            onPressed: () {  },
          ),
          title: InkWell(
            child: Container(
              height: ScreenAdapter.height(70),
              padding: EdgeInsets.only(left: 10),
              decoration: BoxDecoration(
                color: Colors.black12,
                borderRadius: BorderRadius.circular(30),
              ),
              child: Row(
                children: [
                  Icon(Icons.search,color: Colors.black54),
                  Text('笔记本',style: TextStyle(fontSize: ScreenAdapter.size(34),color: Colors.black54))
                ],
              ),
            ),
            onTap: (){
              Navigator.pushNamed(context,'/search');
            },
          ),
          actions:[
            IconButton(
              icon: Icon(Icons.message, size:28,color:Colors.black87),
              onPressed: (){},
            )
          ]
      ),
      body: PageView(
        controller: this._pageController,
        children: _pageList,
        onPageChanged: (index){
          this.setState(() {
            this._currentIndex = index;
          });
        },
          physics: NeverScrollableScrollPhysics(),   // 禁止PageView滑动
      ),
      bottomNavigationBar: BottomNavigationBar(
          fixedColor: Colors.red,
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentIndex,
          backgroundColor: Color.fromRGBO(240, 240, 240, 1),
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
            BottomNavigationBarItem(icon: Icon(Icons.category), label: '分类'),
            BottomNavigationBarItem(icon: Icon(Icons.shop), label: '购物车'),
            BottomNavigationBarItem(icon: Icon(Icons.people), label: '我的'),
          ],
          onTap: (index) {
            setState(() {
              _currentIndex = index;
              this._pageController.jumpToPage(index);
            });
          }),
    );
  }
}
